""" System imports """
import logging

""" Equinox """
from events import *

""" Constants """


""" Set up logging """
_log = logging.getLogger("equinox.character")


class Character:
    """ Represents a character on the screen, in the game """

    STATE_INACTIVE = 0
    STATE_ACTIVE = 1

    def __init__(self, evManager):
        self.evManager = evManager
        self.evManager.registerListener( self )

        self.sector = None
        self.state = Character.STATE_INACTIVE

    #----------------------------------------------------------------------
    def __str__(self):
        return '<Character %s>' % id(self)

    #----------------------------------------------------------------------
    def move(self, direction):
        if self.state == Character.STATE_INACTIVE:
            return
        
        if self.sector.movePossible( direction ):
            newSector = self.sector.neighbors[direction]
            self.sector = newSector
            ev = CharacterMoveEvent( self )
            self.evManager.post( ev )
    #----------------------------------------------------------------------
    def place(self, sector):
        self.sector = sector
        self.state = Character.STATE_ACTIVE

        ev = CharacterPlaceEvent( self )
        self.evManager.post( ev )

    #----------------------------------------------------------------------
    def notify(self, event):
        if isinstance( event, GameStartedEvent ):
            gameMap = event.game.map
            self.place( gameMap.sectors[0][0] )

        elif isinstance( event, CharacterMoveRequest ):
            self.move( event.direction )
